<template>
	<view>
		<view class="content">
			<view class="head">
				<view class="text">枫叶生产管理系统</view>
				<view class="text3">{{version}}</view>
			</view>
		</view>
		<view class="form">
			<view class="item"><input type="text" placeholder="请输入您的帐号" autocomplete="on" class="name" v-model="usernametxt" /></view>
			<view class="item">
				<input type="text" value="" :password="showpassword" placeholder="请输入密码" class="password" v-model="passwordtxt"/>
				<sunui-password @change="showPass" />
			</view>
			<view class="submit"><button class="lbtn" @click="Event_Login()">登&nbsp;陆</button></view>
		</view>
		<view class="footer">
			<text>
				登录使用就表示同意用户协议
				<a href="javascript:;">条款</a>
				和
				<a href="javascript:;">隐私</a>
				政策
			</text>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			usernametxt: '',
			passwordtxt: '',
			cid: '',
			showpassword: true,
			version : '（ V0.0.0 ）'
		};
	},
	onLoad() {
		let username = uni.getStorageSync('loginName');
		let password = uni.getStorageSync('password');
		if (username && password) {
			this.getLoginInfo(username, password);
		};
	},
	onBackPress(e) {
		return fasle;
	},
	methods: {
		getLoginInfo(username, password) {
			uni.showToast({
				title: '开始登录',
				icon: 'none',
				duration: 1000
			});

			//#ifndef H5
			let pinf = plus.push.getClientInfo();
			this.cid = (pinf && pinf.clientid) || '';
			this.version = '（ V'+plus.runtime.version+' ）'
			//#endif

			//console.log('cid:'+this.cid)
			this.$http
				.post('/rest/login', {
					username: username,
					password: password,
					cid: this.cid
				})
				.then(res => {
					//					console.log(JSON.stringify(res))
					uni.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 3000
					});
					if (res.data.code == '0') {
						uni.setStorageSync('userId', res.data.data.userId);
						uni.setStorageSync('deptId', res.data.data.deptId);
						uni.setStorageSync('orgName', res.data.data.deptName);
						uni.setStorageSync('userName', res.data.data.userName);
						uni.setStorageSync('roleKey', res.data.data.roleKey);
						uni.setStorageSync('loginName', this.usernametxt);
						uni.setStorageSync('password', this.passwordtxt);
						uni.setStorageSync('cid', this.cid);
						uni.switchTab({
							url: '../index/index'
						});
					}
				});
		},
		Event_Login() {
			if (this.usernametxt && this.passwordtxt) {
				this.getLoginInfo(this.usernametxt, this.passwordtxt);
			} else {
				uni.showToast({
					title: '请填写用户名密码',
					icon: 'none',
					duration: 3000
				});
			}
		},
		showPass(e) {
			this.showpassword = e;
		}
	}
};
</script>
<style lang="css">
@import "/static/css/login.css";
</style>
